<?php

try {
	if(file_exists("./router/interfaces/interface_loader.php")){
		require_once("./router/interfaces/interface_loader.php");
	}
	else {
		throw new Exception('Nie można załadować pliku: ./router/interfaces/interface_loader.php');
	}
}
catch(Exception $e){
	return $e->getMessage();
}
class Application_Router implements Argument_Parsedable,
									Class_Loadable,
									Class_Controller_Creatable, 
									Class_Loadable,
									Class_Name_Composable,
									Controller_Loadable,
									Path_Controller_Creatable {
	
	public $getVars = array();
	public $postVars = array();
	
	public function load_class($class_name){
		list($name, $something) = preg_split('/_/', strrev($class_name), 2);
		$something = strrev($something);
		$name = strrev($name);
		list($functionality, $folder) = preg_split('/_/', strrev($something), 2);
		$folder = strrev($folder);
		$functionality = strrev($functionality);
		$file_path = $this->class_loader($name, $folder, $functionality);
		return $file_path;
	}
	
	public function compose_class_name($name, $folder, $functionality){
		switch($folder){
			case 'model':
				$file_path = SITE_ROOT . '/' . basename($functionality) . '/' . basename('models') . '/' . basename($name.'.php');
				break;
			case 'data':
				$file_path = SITE_ROOT . '/' . basename($functionality) . '/' . basename('data') . '/' . basename($name.'.php');
				break;
			case 'core':
				$file_path = SITE_ROOT . '/' . basename($functionality) . '/' . basename('core') . '/' . basename($name.'.php');
				break;
			case 'libraries':
				$file_path = SITE_ROOT . '/' . basename($functionality) . '/' . basename('libraries') . '/' . basename($name.'.php');
				break;
			case 'driver':
				$file_path = SITE_ROOT . '/' . basename($functionality) . '/' . basename('libraries') . '/' . basename('drivers') . '/' . basename($name.'.php');
				break;
			case 'other':
				$file_path = SITE_ROOT . '/' . basename($functionality) . '/' . basename('other') . '/' . basename($name.'.php');
				break;
			default:
				throw new Exception('Niepoprawna wartość!');
		}
	}
	
	public function class_loader($name, $folder, $functionality){
		try {
			$file_path = $this->compose_class_name($name, $folder, $functionality);
			if(file_exists($file_path)){
				return $file_path;
			}
			else {
				throw new Exception('Nie można załadować pliku: '.$file_path);
			}
		}
		catch(Exception $e){
			return $e->getMessage();
		}
	}
	
	public function controller_loader($request_string, array $post){
		$parsed_string = explode('&', $request_string);
		$controller_name = array_shift($parsed_string);
		$getVars = array();
		$postVars = array();
		$postVars = $post;
		$getVars = $this->parsed_arguments($parsed_string);
		$controller_path = $this->create_controller_path($controller_name);
		$controller_class = $this->create_controller_class($controller_name);
		$controller_value['controller_path'] = $controller_path;
		$controller_value['controller_class'] = $controller_class;
		$return_array = array();
		$return_array['files_value'] = $controller_value;
		$return_array['post'] = $post;
		$return_array['get'] = $getVars;
		return $return_array;
	}
	
	public function create_controller_class($controller_name){
		try {
			$controller_class = ucfirst($controller_name) . '_Controller_' . ucfirst($controller_name);
			if(class_exists($controller_class)){
				return $controller_class;
			}
			else {
				throw new Exception('Nie można załadować klasy kontrolera: '.$controller_name);
			}
		}
		catch(Exception $e){
			return $e->getMessage();
		}
	}
	
	public function create_controller_path($controller_name){
		try {
			$controller_path 	= SITE_ROOT 
								. '/' . 
								basename('functionalities')
								. '/' .
								basename($controller_name)
								. '/' .
								'/controllers/'
								. $controller_name.
								'.php';
			if(file_exists($controller_path)){
				return $controller_path;
			}
			else {
				throw new Exception('Niestety nie udało się załadować kontrolera. Wybrany plik nie istnieje!');
			}
								 
		}
		catch(Exception $e){
			return $e->getMessage();
		}
	}
	
	public function parsed_arguments($parsed_string){
		$getVars = array();
		foreach($parsed_string as $argument){
			list($variable, $value) = explode('=', $argument);
			$getVars[$variable] = urldecode($value); 
		}
		return $getVars;
	}
}